package com.jim.director.framework.core;

import com.jim.director.framework.ioc.ActorFactory;
import com.jim.director.framework.log.LogSystem;
import com.jim.director.framework.simulation.SimulationContext;
import com.jim.director.framework.validation.AnnotationValidationEngine;
import com.jim.director.framework.validation.AnnotationValidationException;
import com.jim.director.framework.validation.ValidationReport;

/**
 * author: Jim
 * date: 2024/10/2
 * info:
 */

public class DirectorApplication {

    public static void run(Class<?> starter){
        System.out.println("Director框架启动中...");

        // 1. 注解校验阶段 - 新增
        System.out.println("正在进行注解校验...");
        AnnotationValidationEngine validationEngine = new AnnotationValidationEngine();
        ValidationReport report = validationEngine.validateAll(starter);

        // 检查校验结果
        if (!report.isValid()) {
            System.err.println("❌ 注解校验失败！");
            System.err.println(report.generateReport());
            throw AnnotationValidationException.withDetailedMessage(report);
        }

        // 输出警告信息（如果有）
        if (report.hasWarnings()) {
            System.out.println("⚠️  注解校验发现警告信息：");
            for (String warning : report.getAllWarnings()) {
                System.out.println("  ⚠️  " + warning);
            }
            System.out.println();
        }

        if (report.getTotalClasses() > 0) {
            System.out.println("✅ 注解校验通过！");
        }

        //2. 加载actor
        ActorFactory actorFactory = new ActorFactory();
        try{
            actorFactory.loadActors(starter);
        }catch (Exception e){
            e.printStackTrace();
            System.err.println("❌ Actor加载失败: " + e.getMessage());
            return;
        }

        //3. 加载Log
        LogSystem logSystem = new LogSystem();
        logSystem.loadLog(actorFactory);

        //4. 加载仿真环境
        SimulationContext simulationContext = new SimulationContext();
        simulationContext.loadSimulation(actorFactory, logSystem);

        //5. 开始仿真
        System.out.println("🚀 开始仿真...");
        simulationContext.start();
        System.out.println("✅ 仿真完成！");
    }
}
